package edu.strategy.single.scanned;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import robocode.AdvancedRobot;
import robocode.Event;
import robocode.ScannedRobotEvent;
import robocode.util.Utils;
import edu.strategy.single.Single;

public class SavantMicroScannedStrategy extends Single{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static final int maxMatchLen = 30;
	final static int DISTANCEDIVISOR=150;
	final static Rectangle2D.Double fieldRect=new Rectangle2D.Double(0,0,800,600);
	static double hits[][][]=new double[17][17][20];
	static double enemyEnergy;
	static int oldMax;
	static int currentMax;
	static int distSeg;
	static boolean enemyIsAlive;
	
	public SavantMicroScannedStrategy(AdvancedRobot advancedRobot) {
		super(advancedRobot);
	}
	
	@Override
	public void prepare() {
		enemyIsAlive = true;
	}
	
	@Override
	public void work() {
		super.getAdvancedRobot().fire(1);	
	}
	@Override
	public void work(Event e) {
		super.used();
		ScannedRobotEvent srv = (ScannedRobotEvent) e;
		
		double absBearing;
		int matchLen = maxMatchLen;
		int matchPos;
		int i;
		double firePower=(srv.getDistance()>70?(Math.min(Math.min(srv.getEnergy()/4, super.getAdvancedRobot().getEnergy()/15),(srv.getDistance()>150?2.5:3))):3);
		distSeg=(int)(srv.getDistance()/DISTANCEDIVISOR);
		if((absBearing=enemyEnergy-(enemyEnergy=srv.getEnergy()))>=0.1&&absBearing<=3){
			oldMax=currentMax+8;
			chooseVel();
		}
		super.getAdvancedRobot().setMaxVelocity((srv.getDistance()>70?Math.abs(currentMax):8));
		super.getAdvancedRobot().setAhead(currentMax*Double.POSITIVE_INFINITY);
		super.getAdvancedRobot().setTurnRightRadians(Utils.normalRelativeAngle((absBearing=srv.getBearingRadians())+Math.PI/2)+currentMax*(250-srv.getDistance())/2000);
		super.getAdvancedRobot().setTurnRadarRightRadians(Utils.normalRelativeAngle((absBearing+=super.getAdvancedRobot().getHeadingRadians())-super.getAdvancedRobot().getRadarHeadingRadians())*2);

		super.getAdvancedRobot().setFire(firePower);

		//WeekendObsession gun
		enemyHistory =String.valueOf((char) (srv.getVelocity() * Math.sin(srv.getHeadingRadians() - absBearing))).concat(enemyHistory);

		while((matchPos = enemyHistory.indexOf(enemyHistory.substring(0, matchLen--), i = (int)( ( srv.getDistance()) / (20-3*Math.max(0.1,Math.min(3,firePower))))))< 0);

		do {
			absBearing += (short) enemyHistory.charAt(--matchPos) / srv.getDistance();
		} while (--i > 0);
		super.getAdvancedRobot().setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - super.getAdvancedRobot().getGunHeadingRadians()));
	}
	
	private void chooseVel(){
		double maxRating=Double.POSITIVE_INFINITY;
		for(int i=0;i<17;i++){
			double rating=0;
			for(int a=0;a<17;a++){
				for(int b=0;b<17;b++){
					for(int c=0;c<20;c++){
						rating+=((hits[a][b][c]/(Math.abs(i-a)==0?0.5:Math.abs(i-a)))/(Math.abs(oldMax-b)==0?0.5:Math.abs(oldMax-b)))/
						(Math.abs(distSeg-c)==0?0.5:Math.abs(distSeg-c));
					}
				}
			}
			if(rating<maxRating&&fieldRect.contains(project((i-8)*50,super.getAdvancedRobot().getHeadingRadians()))){
				maxRating=rating;
				currentMax=i-8;
			}
		}
	}
	
	private Point2D.Double project(double dist,double angle){
		return new Point2D.Double(super.getAdvancedRobot().getX()+dist*Math.sin(angle),super.getAdvancedRobot().getY()+dist*Math.cos(angle));
	}
	
	static String enemyHistory = ""
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 1
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 2
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char)-1
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char) 0 + (char) 0 + (char) 0
		+ (char) 0 + (char)-2 + (char)-4 + (char)-6
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-8 + (char)-8 + (char)-8 + (char)-8
		+ (char)-7 + (char)-6 + (char)-5 + (char)-4
		+ (char)-3 + (char)-2 + (char)-1 + (char)0
		+ (char) 2 + (char) 4 + (char) 6 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 8 + (char) 8 + (char) 8 + (char) 8
		+ (char) 7 + (char) 6 + (char) 5 + (char) 4
		+ (char) 3 + (char) 2 + (char) 1 + (char) 0;


	@Override
	public String toString() {
		return "SavantMicro";
	}
}


